package com.zabra.zl.ui.deposit;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.DepositParmasBean;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.data.MyCityBean;
import com.zabra.zl.myview.SwipeItemLayout;
import com.zabra.zl.ui.addbank.AddBankActivity;
import com.zabra.zl.ui.deposit.contract.DepositContract;
import com.zabra.zl.ui.deposit.presenter.DepositPresenter;
import com.zabra.zl.ui.inputpwd.InputPwdActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.GetJsonDataUtil;
import com.zabra.zl.utils.HideUtil;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 提现
 */
public class DepositActivity extends BaseMvpActivity<DepositPresenter> implements DepositContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_isdeposit)
    TextView tvIsdeposit;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.iv_bank_icon)
    ImageView ivBankIcon;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.tv_bank_type)
    TextView tvBankType;
    @BindView(R.id.tv_bank_id)
    TextView tvBankId;
    @BindView(R.id.ll_showbank)
    LinearLayout llShowbank;
    @BindView(R.id.ll_addbank)
    LinearLayout llAddbank;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    private List<DepositParmasBean.CardsBean> list;
    private Bundle bundle=new Bundle();
    private String strMoney,strBank,strBankId;
    private Dialog dialog;
    @Override
    public int getLayoutId() {
        return R.layout.activity_deposit;
    }

    @Override
    public void initView() {
        tvTitle.setText("提现");
        mPresenter = new DepositPresenter();
        mPresenter.attachView(this);
        keyList.add("debtorId");
        valueList.add(strUserid);
        mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));

    }


    @Override
    public void showLoading() {
        dialog=DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(DepositParmasBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            strMoney=bean.getBalance();
            tvMoney.setText(strMoney);
            tvIsdeposit.setText("可提余额" + strMoney+ ",");
            list = bean.getCards();
            if (list.size() == 0) {
                //没有银行卡
                llAddbank.setVisibility(View.VISIBLE);
                llShowbank.setVisibility(View.GONE);
                tvSure.setAlpha(0.5f);
                tvSure.setEnabled(false);
            } else {
                //有银行卡
                strBank=list.get(0).getBankName();
                strBankId=list.get(0).getCardNo();
                llAddbank.setVisibility(View.GONE);
                llShowbank.setVisibility(View.VISIBLE);
                tvBank.setText(strBank);
                tvBankId.setText(HideUtil.hideCardNo(strBankId));
                tvSure.setAlpha(1);
                tvSure.setEnabled(true);
            }
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }


    @OnClick({R.id.back, R.id.tv_all, R.id.ll_showbank, R.id.ll_addbank, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_all:
                //全部提现
                etMoney.setText(strMoney);
                break;
            case R.id.ll_showbank:
                //选择银行卡
                showBottomDialog();
                break;
            case R.id.ll_addbank:
                //添加银行卡
                bundle.putString("isdeal","no");
                toClass1(view.getContext(), AddBankActivity.class,bundle);
                EventMessage message = new EventMessage(1, "Deposit");
                EventBus.getDefault().postSticky(message);
                toClass1(this, AddBankActivity.class);
                break;
            case R.id.tv_sure:
                //确认提现
                String trim = etMoney.getText().toString().trim();
                bundle.putString("money",trim);
                bundle.putString("bankid",strBankId);
                toClass1(this,InputPwdActivity.class,bundle);
                break;
        }
    }

    private void showBottomDialog() {
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        View view = View.inflate(this, R.layout.dialog_choosebank, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        RecyclerView recyclerView = view.findViewById(R.id.dilog_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));
        final Adp adp = new Adp(R.layout.item_bank_list, list);
        recyclerView.setAdapter(adp);
        adp.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.item_view:
                        strBank=list.get(position).getBankName();
                        strBankId=list.get(position).getCardNo();
                        tvBank.setText(strBank);
                        tvBankId.setText(HideUtil.hideCardNo(strBankId));
                        dialog.dismiss();
                        break;
                    case R.id.delete:
                        //左滑删除
                        list.remove(position);
                        adp.notifyDataSetChanged();
                        break;
                }
            }
        });


        view.findViewById(R.id.dialog_addback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加银行卡
                bundle.putString("isdeal","yes");
                toClass1(view.getContext(), AddBankActivity.class,bundle);
                EventMessage message = new EventMessage(1, "Deposit");
                EventBus.getDefault().postSticky(message);
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.iv_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

    }


}
